How to catch API call errors in python